package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: yuanZhi...
 * @Date: created in  2021/12/19 17:29
 */
public class CommonRange {
    public static void main(String[] args) {
        List<List<Integer>> lists = new ArrayList<>();
        List<Integer> list0 = new ArrayList<>();
        list0.add(0);
        list0.add(3);
        lists.add(list0);
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(3);
        lists.add(list1);
        List<Integer> list2 = new ArrayList<>();
        list2.add(3);
        list2.add(5);
        lists.add(list2);
        List<Integer> list3 = new ArrayList<>();
        list3.add(3);
        list3.add(6);
        lists.add(list3);

        List<List<Integer>> result = get(lists);
        System.out.println(result);
    }

    private static List<List<Integer>> get(List<List<Integer>> lists) {
        // 找公共区间集合
        List<List<Integer>> resultLists = new ArrayList<>();
        for (int i = 0; i < lists.size(); i++) {
            List<Integer> list = lists.get(i);
            for (int j = i + 1; j < lists.size(); j++) {
                List<Integer> compareList = lists.get(j);
                if (list.get(1) >= compareList.get(0)) {
                    List<Integer> resultList = new ArrayList<>();
                    if (list.get(0) <= compareList.get(0)
                            && list.get(1) <= compareList.get(1)) {
                        resultList.add(compareList.get(0));
                        resultList.add(list.get(1));
                    }
                    if (list.get(0) <= compareList.get(0)
                            && list.get(1) > compareList.get(1)) {
                        resultList.add(compareList.get(0));
                        resultList.add(compareList.get(1));
                    }
                    resultLists.add(resultList);
                }
            }
        }

        // 找交集
        if (resultLists.size() > 1) {
            List<List<Integer>> finalResultLists = new ArrayList<>();
            for (int i = 0; i < resultLists.size(); i++) {
                List<Integer> list = resultLists.get(i);
                List<Integer> tempList = new ArrayList<>();
                HashMap<Integer, Integer> map = new HashMap<>();
                for (int j = i + 1; j < resultLists.size(); j++) {
                    List<Integer> compareList = resultLists.get(j);
                    if (list.get(1).equals(compareList.get(0))) {
                        map.put(1, list.get(0));
                        map.put(2, compareList.get(1));
                    }
                }
                map.forEach((key, value) -> tempList.add(value));
                if (tempList.size() > 0) {
                    finalResultLists.add(tempList);
                }
            }
            if (finalResultLists.size() > 0) {
                return finalResultLists;
            }
        }
        return resultLists;
    }
}
